package controller

import (
	"encoding/json"
	"os"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/aquasecurity/trivy-operator/pkg/apis/aquasecurity/v1alpha1"
)

func TestIsSbomExceededSecretSizeLimit(t *testing.T) {
	testCases := []struct {
		name        string
		bomFilePath string
		size        int
		want        bool
	}{
		{
			name:        "bom with valid size",
			bomFilePath: "./testdata/sbom.json",
			size:        5000,
			want:        false,
		},
		{
			name:        "bom with bad size",
			bomFilePath: "./testdata/sbom.json",
			size:        4900,
			want:        true,
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			b, err := os.ReadFile(tc.bomFilePath)
			require.NoError(t, err)
			var sbom v1alpha1.ClusterSbomReport
			err = json.Unmarshal(b, &sbom)
			require.NoError(t, err)
			got := isSbomExceededSecretSizeLimit(sbom.Report.Bom, tc.size)
			assert.Equal(t, tc.want, got)

		})
	}
}
